package minisoft.application.loterryservice;

import java.util.Calendar;
import java.util.List;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;

public class SearchActivity extends Activity {
	private ListView mainListView;
	private Prize prize;
	private int int_region = 1;
	private Calendar date_search = Calendar.getInstance();
	private TextView north;
	private TextView middle;
	private TextView south;
	private TextView today;
	private TextView date_title;
	private TextView[] province;

	private void regionProcess(int region, final SearchAdapter searchAdapter) {
		// define number of provinces in this region by date
		TodayDataSource todayDS = new TodayDataSource();
		List<Today> today_list = todayDS.searchTodaysByDateInWeekAndRegion(
				this, date_search.get(Calendar.DAY_OF_WEEK), int_region);

		final int count = today_list.size();

		if (count > 1) {
			LinearLayout province_header = (LinearLayout) findViewById(R.id.province_header);
			if(((LinearLayout) province_header).getChildCount() > 0) 
			    ((LinearLayout) province_header).removeAllViews(); 

			TextView tvi = new TextView(this);
			// tvi.setText(today_list.get(i).getProvinceName());
			int resId = getResources().getIdentifier(
					"prize_name" + String.valueOf(count), "style",
					getPackageName());
			tvi.setTextAppearance(getApplicationContext(), resId);

			tvi.setGravity(Gravity.CENTER);
			tvi.setTextColor(getResources().getColor(R.color.White));
			tvi.setBackgroundColor(getResources().getColor(R.color.Gray));

			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, 0.15f);

			tvi.setLayoutParams(params);

			tvi.setTextSize(15f);
			tvi.setPadding(5, 5, 5, 5);
			province_header.addView(tvi);

			province = new TextView[count];
			for (int i = 0; i < count; ++i) {

				province[i] = new TextView(this);

				province[i].setText(today_list.get(i).getProvinceName());
				resId = getResources().getIdentifier(
						"prize_value" + String.valueOf(count), "style",
						getPackageName());
				province[i].setTextAppearance(getApplicationContext(), resId);
				province[i].setGravity(Gravity.CENTER);
				params = new LinearLayout.LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT,
						(float) (0.85 / count));

				province[i]
						.setTextColor(getResources().getColor(R.color.White));
				province[i].setBackgroundColor(getResources().getColor(
						R.color.Gray));

				province[i].setLayoutParams(params);
				if (i == 0) {
					province[i].setBackgroundResource(R.color.HotPink);
				}

				province[i].setTextSize(15f);
				province[i].setPadding(5, 5, 5, 5);

				province_header.addView(province[i]);

				province[i].setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						TextView tx=(TextView)v;
						
						for (int i = 0; i < count; ++i) {
							province[i].setBackgroundResource(R.color.Gray);
						}
						tx.setBackgroundResource(R.color.HotPink);
						prize = SearchDataSource.searchPrizesByProvinceAndDate(
								int_region, date_search);
						
						searchAdapter.setPrizeArray(prize.getPrizeArray());
						searchAdapter.notifyDataSetChanged();
					}
				});
			}
		}else
		{
			LinearLayout province_header = (LinearLayout) findViewById(R.id.province_header);
			if(((LinearLayout) province_header).getChildCount() > 0) 
			    ((LinearLayout) province_header).removeAllViews(); 
		}

		String region_title = "";
		switch (region) {
		case 1:
			region_title = getResources().getString(
					R.string.title_retion_result_north);
			north.setBackgroundResource(R.color.HotPink);
			middle.setBackgroundResource(R.color.Gray);
			south.setBackgroundResource(R.color.Gray);
			break;
		case 2:
			region_title = getResources().getString(
					R.string.title_retion_result_middle);
			middle.setBackgroundResource(R.color.HotPink);
			north.setBackgroundResource(R.color.Gray);
			south.setBackgroundResource(R.color.Gray);
			break;
		case 3:
			region_title = getResources().getString(
					R.string.title_retion_result_south);
			south.setBackgroundResource(R.color.HotPink);
			middle.setBackgroundResource(R.color.Gray);
			north.setBackgroundResource(R.color.Gray);
			break;
		}

		today.setText(region_title);
		date_title
				.setText(getResources().getStringArray(R.array.day_of_week)[date_search
						.get(Calendar.DAY_OF_WEEK)]
						+ ", "
						+ String.valueOf(date_search.get(Calendar.DAY_OF_MONTH))
						+ "/"
						+ String.valueOf(date_search.get(Calendar.MONTH))
						+ "/" + String.valueOf(date_search.get(Calendar.YEAR)));
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_search);
		
		// /TODO get today, call prize from ws and check connection available or
		// not
		boolean connection_available = true;

		if (connection_available) {
			UserDataSource userDS = new UserDataSource(this);
			int_region = userDS.getRegionId();

			if (int_region < 1) {
				int_region = 1;
			}			

			// define region
			// setContentView(R.layout.activity_search);
			today = (TextView) findViewById(R.id.today);
			date_title = (TextView) findViewById(R.id.date);
			north = (TextView) findViewById(R.id.north);
			middle = (TextView) findViewById(R.id.middle);
			south = (TextView) findViewById(R.id.south);

			prize = SearchDataSource.searchPrizesByProvinceAndDate(int_region,
					date_search);
			final SearchAdapter searchAdapter = new SearchAdapter(
					getApplicationContext(), prize);

			// region process
			regionProcess(int_region, searchAdapter);

			north.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					int_region = 1;

					regionProcess(int_region, searchAdapter);

					prize = SearchDataSource.searchPrizesByProvinceAndDate(
							int_region, date_search);

					searchAdapter.setPrizeArray(prize.getPrizeArray());
					searchAdapter.notifyDataSetChanged();

				}
			});

			middle.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {

					int_region = 2;
					regionProcess(int_region, searchAdapter);

					prize = SearchDataSource.searchPrizesByProvinceAndDate(
							int_region, date_search);

					searchAdapter.setPrizeArray(prize.getPrizeArray());
					searchAdapter.notifyDataSetChanged();
				}
			});

			south.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {

					int_region = 3;
					regionProcess(int_region,searchAdapter);

					prize = SearchDataSource.searchPrizesByProvinceAndDate(
							int_region, date_search);

					searchAdapter.setPrizeArray(prize.getPrizeArray());
					searchAdapter.notifyDataSetChanged();
				}
			});

			// Find the ListView resource.
			mainListView = (ListView) findViewById(R.id.mainListView);

			mainListView.setAdapter(searchAdapter);

			mainListView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {

					// ListView Clicked item index
					int itemPosition = position;

					// ListView Clicked item value
					String itemValue = "aaa";// (String)
												// mainListView.getItemAtPosition(position);

					// Show Alert
					Toast.makeText(
							getApplicationContext(),
							"Position :" + itemPosition + "  ListItem : "
									+ itemValue, Toast.LENGTH_LONG).show();

				}

			});
		}
	}
}
